package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.entity.RespVo.AttrRespVo;
import com.atguigu.gulimall.product.entity.vo.AttrVo;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    private AttrAttrgroupRelationDao aard;
    @Autowired
    private AttrGroupDao agd;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr,attrEntity);
        System.out.println(attr);
        this.save(attrEntity);

        AttrAttrgroupRelationEntity aare = new AttrAttrgroupRelationEntity();
        aare.setAttrId(attrEntity.getAttrId());
        aare.setAttrGroupId(attr.getAttrGroupId());

        aard.insert(aare);
    }

    @Transactional
    @Override
    public PageUtils baseList(Map<String, Object> params,Long catelogId) {
        String key = (String)params.get("key");
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if(!key.isEmpty())
            wrapper.like("attr_name",key).or().eq("attr_id",key);
        if(catelogId != 0)
            wrapper.eq("catelot_id",catelogId);

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );

        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> records = page.getRecords();

        List<AttrRespVo> collect = records.stream().map((m) -> {
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(m,attrRespVo);
            //关系表查询关联
            AttrAttrgroupRelationEntity attr_id = aard.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
                    .eq("attr_id", attrRespVo.getAttrId()));
            //
            if (attr_id.getAttrGroupId() != null) {
                //分组表
                AttrGroupEntity attr_group = agd.selectOne(new QueryWrapper<AttrGroupEntity>().
                        eq("attr_group_id", attr_id.getAttrGroupId()));
                attrRespVo.setGroupName(attr_group.getAttrGroupName());

                if (attr_group.getCatelogId() != null) {
                    //分类表
                    CategoryEntity categoryEntity = categoryDao.selectOne(new QueryWrapper<CategoryEntity>()
                            .eq("cat_id", attr_group.getCatelogId()));
                    attrRespVo.setCatelogName(categoryEntity.getName());
                }
            }
            return attrRespVo;
        }).collect(Collectors.toList());


        pageUtils.setList(collect);
        return pageUtils;
    }

}